/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.library.dao.smp;

import br.gov.cemaden.library.dao.GenericDao;
import br.gov.cemaden.library.interfaces.smp.PIDAplicInterface;
import br.gov.cemaden.library.model.smp.PIDAplic;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author paulo.dias
 */
public class PIDAplicDao extends GenericDao implements PIDAplicInterface {

    private Session session;

    public PIDAplicDao(Session session) {
        this.session = session;
    }

    public List<PIDAplic> findAll() {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<PIDAplic> pidAplic = null;
        try {
            Query query = session.getNamedQuery("PIDAplic.findAll");
            pidAplic = (List<PIDAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no PIDAplicDao.findAll: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return pidAplic;
    }

    public PIDAplic findById(Long id) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        PIDAplic pidAplic = null;
        try {
            Query query = session.getNamedQuery("PIDAplic.findById").setParameter("id", id);
            pidAplic = (PIDAplic) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no PIDAplicDao.findById: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return pidAplic;
    }

    public List<PIDAplic> findByNomeProcesso(String nomeProcesso) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<PIDAplic> pidAplic = null;
        try {
            Query query = session.getNamedQuery("PIDAplic.findByNomeProcesso").setParameter("nomeProcesso", nomeProcesso);
            pidAplic = (List<PIDAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no PIDAplicDao.findById: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return pidAplic;
    }

    public PIDAplic findByNomeProcess(String nomeProcesso) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        PIDAplic pidAplic = null;
        try {
            Query query = session.getNamedQuery("PIDAplic.findByNomeProcesso").setParameter("nomeProcesso", nomeProcesso);
            pidAplic = (PIDAplic) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no PIDAplicDao.findByNomeProcess: " + e.getMessage() + " - Causa: " + e.getCause());
            //throw new HibernateException(e);
            e.printStackTrace();
        }
        return pidAplic;
    }

    public PIDAplic insert(PIDAplic pidAplic) {
        PIDAplic aplic = null;
        try {
            aplic = (PIDAplic) saveOrUpdate(this.session, pidAplic);
        } catch (Exception ex) {
            Logger.getLogger(PIDAplicDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return aplic;
    }

    public PIDAplic update(PIDAplic pidAplic) {
        try {
            saveOrUpdate(this.session, pidAplic);
        } catch (Exception ex) {
            Logger.getLogger(PIDAplicDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return pidAplic;
    }

    public void delete(PIDAplic pidAplic) {
        try {
            remove(this.session, pidAplic);
        } catch (Exception ex) {
            Logger.getLogger(PIDAplicDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
